import { makeAutoObservable } from 'mobx';
import { Bean } from '../beans';
import { Todo, TodoState } from '../types/todo';

@Bean('todoStore')
export class TodoStore {
  private state: TodoState = {
    todos: [],
    filter: 'all'
  };

  constructor() {
    makeAutoObservable(this);
  }

  get todos() {
    return this.state.todos;
  }

  get filteredTodos() {
    switch (this.state.filter) {
      case 'active':
        return this.todos.filter(todo => !todo.completed);
      case 'completed':
        return this.todos.filter(todo => todo.completed);
      default:
        return this.todos;
    }
  }

  addTodo(title: string) {
    const todo: Todo = {
      id: Date.now().toString(),
      title,
      completed: false,
      createTime: Date.now()
    };
    this.state.todos.push(todo);
  }

  removeTodo(id: string) {
    this.state.todos = this.state.todos.filter(todo => todo.id !== id);
  }

  toggleTodo(id: string) {
    const todo = this.state.todos.find(todo => todo.id === id);
    if (todo) {
      todo.completed = !todo.completed;
    }
  }

  setFilter(filter: TodoState['filter']) {
    this.state.filter = filter;
  }
} 